package com.genius.unite_platform.device.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.unite_platform.device.entity.DeviceEntity;
import com.genius.unite_platform.device.service.DeviceServiceAdapter;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;

import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service("DeviceDeleteService")
public class DeviceDeleteServiceImpl extends  DeviceServiceAdapter {
	@Override
	public JSONObject batchdel(JSONObject json, JSONObject response) {		
				
		String whereField = DeviceEntity.deviceno;
    	String delTable = "device";
    	try {
    		JSONArray  arr= JSONArray.parseArray(json.getString("delData"));  
    		
    		for(int i=0;i<arr.size();i++){
		     	JSONObject obj = arr.getJSONObject(i);	   		   
			
    	       JSONObject existJson = DBTools2.getIsExist("deviceno","devicemodel", obj);			
    	    	if(existJson != null){
    		    	response.put(ResponseJson.msg, obj.getString("deviceno" )+"存在陈列设备类型，不可以直接删除！");
    		    	response.put(ResponseJson.status, ResponseJson.status_Failed);
    		    	return response;
    		     }	
   		
    		   JSONObject existJson1 = DBTools2.getIsExist("deviceno","storedevice", json);			
    		    if(existJson1 != null){
    			response.put(ResponseJson.msg, obj.getString("deviceno" )+"存在分店陈列设备，不可以直接删除！");
    			response.put(ResponseJson.status, ResponseJson.status_Failed);
    			return response;
    		  }	
    		}    		
			DBTools.delete(whereField, delTable,json);
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Del_Data_Success);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Del_Data_Error);
		}
		return response;
	}

}


	